//
//  ControlTemplateViewProblemClassController.m
//  Uhomebk
//
//  Created by 揭婧雯 on 17/3/3.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "ControlTemplateViewProblemClassController.h"
#import "ProblemClassViewController.h"
#pragma mark -
#pragma mark 问题归类控件 【problem_class】
@interface ControlTemplateViewProblemClassController()<ProblemSelectDelegate>
{
    UILabel* contentLabel;
    CGSize titleSize;
}
@property (nonatomic, retain) NSString* defaultValue;

@end

@implementation ControlTemplateViewProblemClassController

static ControlTemplateViewProblemClassController *sharedManagerProblemClass  = nil;

+ (ControlTemplateViewProblemClassController *)sharedManagerProblemClass
{
    sharedManagerProblemClass = [[self alloc] init];
    
    return sharedManagerProblemClass;
}

/**
 *  状态控件变量初始化
 */
- (void)initVar
{
    _valueStr = @"";
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    _initdata = [_params valueForKey:@"initData"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initVar];
    self.isNeedAdpterHotspot = YES;
    
    _bgMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, TEMPLATE_HEIGHT)];
    _bgMainView.backgroundColor = kColor_W;
    self.tempBgView = _bgMainView;
    [self.view addSubview:_bgMainView];
    
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    
    UILabel* titleLabel = [[UILabel alloc] init];
    [titleLabel extendAttributedString:[_params valueForKey:@"title"] lineSpace:LINESPACE_6 font:kFont_16];    [titleLabel setFont:kFont_16];
    [titleLabel setTextColor:kColor_1];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [_bgMainView addSubview:titleLabel];
    [titleLabel sizeToFit];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgMainView.mas_left).offset(HEADSPACE);
        make.centerY.equalTo(_bgMainView.mas_centerY);
        make.width.mas_equalTo(TITLE_WIDTH);
    }];
    titleSize = [[_params valueForKey:@"title"] getAttributionHeightWithlineSpace:LINESPACE_6 font:kFont_16 width:TITLE_WIDTH];
    
    //判断defaultValue是否有值
    if ([[_params valueForKey:@"attrValue"] length] > 0) {
        _defaultValue = [_params valueForKey:@"attrValue"];
    }
    
    //是否可读 0-不显示 1-显示
    if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"0"]) {
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 0);
    }
    else {
        //线条
        UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(LINEMARGIN_LEFT, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(_bgMainView.bounds), 0.5)];
        lineView.backgroundColor = kColor_L2;
        lineView.tag = 8888;
        [_bgMainView addSubview:lineView];
    }
    
    //是否必填 //是否可读 1-必须 0-可不填
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    
    ////////是否可写 //是否可读 0-不可写 1-可写
    if ([[SEGUtils ValueToString:[_params valueForKey:@"writable"]] isEqualToString:@"0"]) //当writable为0时可读
    {
        
        NSDictionary * attrValueDic = [SEGUtils dictionaryWithJsonString:_params[@"attrValue"]];
        self.valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT)];
        [_valueLabel setText:attrValueDic[@"value"]];
        [_valueLabel setFont:kFont_16];
        [_valueLabel setTextColor:kColor_1];
        [_valueLabel setTextAlignment:NSTextAlignmentLeft];
        [_bgMainView addSubview:_valueLabel];
        
    }else{
        //箭头
        UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_more_gray")];
        UIImageView* iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(_bgMainView.bounds) - image.size.width - HEADSPACE, (CGRectGetHeight(_bgMainView.bounds) - image.size.height) / 2, image.size.width, image.size.height)];
        [iconImageView setImage:image];
        [_bgMainView addSubview:iconImageView];
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_bgMainView.mas_centerY);
            make.right.equalTo(_bgMainView.mas_right).offset(-HEADSPACE);
        }];
        abtn = [[CustomButtonView alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT, TEMPLATE_HEIGHT)];
        [abtn addTarget:self action:@selector(onBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_bgMainView addSubview:abtn];
        [abtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_bgMainView);
        }];
        //显示值
        self.valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT -2*CONTENTMARGIN_RIGHT - image.size.width, TEMPLATE_HEIGHT)];
        [_valueLabel setText:[_params valueForKey:@"markedWords"]];
        [_valueLabel setFont:kFont_16];
        [_valueLabel setTextColor:kColor_C];
        [_valueLabel setTextAlignment:NSTextAlignmentLeft];
        [_bgMainView addSubview:_valueLabel];
        
        titleSize.height = titleSize.height > 24 ? titleSize.height : 24;
        _valueLabel.frame = CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT -2*CONTENTMARGIN_RIGHT - image.size.width, TEMPLATE_HEIGHT -24 + titleSize.height);
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), CGRectGetHeight(_valueLabel.bounds));
        UIView* lineView = [_bgMainView.subviews objectAtIndex:1];
        lineView.frame = CGRectMake(lineView.frame.origin.x, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(lineView.bounds), CGRectGetHeight(lineView.bounds));
    }
}

- (void)onBtnAction:(CustomButtonView*)sender
{
    
    ProblemClassViewController *problemVC = [[ProblemClassViewController alloc] init];
    problemVC.title = [_params valueForKey:@"title"];
    problemVC.dataArray = [SEGUtils arrayWithJsonString:_initdata];
    problemVC.fundelegate = self;
    [self.masterVC.navigationController pushViewController:problemVC animated:YES];
}

- (void)returnSelectedData:(NSDictionary *)params dataArray:(NSArray *)dataArray
{
    NSString *issueCategoryId = CHECKVALUE([SEGUtils ValueToString:params[@"issueCategoryId"]]);
    NSString *issueCategoryValue = CHECKVALUE([SEGUtils ValueToString:params[@"issueCategoryValue"]]);
    NSDictionary * attrValueDic = [[NSDictionary alloc] initWithObjectsAndKeys:issueCategoryId,@"id",issueCategoryValue,@"value", nil];
    self.valueStr = [attrValueDic yy_modelToJSONString];
    self.optional = @"0";
    _initdata = [dataArray yy_modelToJSONString];
    self.valueLabel.text = issueCategoryValue;
    [_valueLabel setTextColor:kColor_1];
    
}

/**
 回填缓存值
 
 @param attrValue
 */
-(void)paddProblem_classCacheWith:(NSString *)attrValue
{
    if (attrValue.length == 0) {
        return;
    }
    
    NSDictionary *dict = [SEGUtils dictionaryWithJsonString:attrValue];
    NSString *textStr =  dict[@"value"];
    _valueStr = attrValue;
    [_valueLabel setText:textStr];
    [_valueLabel setTextColor:kColor_1];
    self.optional = @"0";
}



@end

